package com.youhui.weixin.service;

import com.youhui.weixin.common.MythConfig;
import com.youhui.weixin.dao.GoodsDao;
import com.youhui.weixin.dao.GoodsLinkDao;
import com.youhui.weixin.domain.Goods;
import com.youhui.weixin.domain.GoodsLink;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.youhui.weixin.common.MythConfig.SHORT_MAP;
import static com.youhui.weixin.common.MythConfig.TYPE_DICT;
import static com.youhui.weixin.common.MythConfig.TYPE_SPACE;

/**
 * 商品service.
 * User: huang
 * Date: 17-10-20
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsDao goodsDao;
//    @Autowired
//    private GoodsLinkDao goodsLinkDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;


    /**
     * 短链接映射得到长链接
     * @param uuid 短链接中特政符
     * @return 真实URL
     */
    public String shortMapLong(String uuid){
//        goodsLinkDao.increase();
        String longLink = (String) stringRedisTemplate.opsForHash().get(SHORT_MAP, uuid);
        if (longLink == null){
            longLink = (String) stringRedisTemplate.opsForHash().get(SHORT_MAP, MythConfig.SHORT_LINK+uuid);
        }
        System.out.println(uuid+"____"+longLink);
        return longLink;
    }
    /**
     * 批量删除，有一个错误的话就中断，返回失败
     */
    public boolean deleteBatch(String idList) {
        idList = idList.trim().substring(1, idList.length() - 1);
        String[] goodList = idList.split(",");
        for (String id : goodList) {
            boolean flag = delete(Long.parseLong(id));
            if (!flag) {
                return false;
            }
        }
        return true;
    }

    /**
     * 删除一个，成功true，失败false
     */
    public boolean delete(Long goodsId) {
        goodsDao.delete(goodsId);
        long result = stringRedisTemplate.opsForHash().delete(MythConfig.GOODS_KEY, goodsId + "");
        return result == 1;
    }

    /**
     * 分页获取热门商品.
     *
     * @param pageNum 要获取的页码
     * @return 商品集合
     */
    public Page<Goods> getHotGoods(int pageNum) {
        Pageable pageable = new PageRequest(pageNum, MythConfig.HOT_GOODS_SIZE);
        return goodsDao.getHotGoods(pageable);
    }

    /**
     * 分页获取某类型商品.
     *
     * @param typeId  类型
     * @param pageNum 页码
     * @return
     */
    public Page<Goods> getTypeGoods(int typeId, int pageNum) {
        Pageable pageable = new PageRequest(pageNum, MythConfig.HOT_GOODS_SIZE);
        return goodsDao.getTypeGoods(typeId, pageable);
    }

    /**
     * 返回页面分页获取某类型商品.
     * @param typeId
     * @param pageNum
     * @return
     */
    public Page<Goods> getHistoryTypeGoods(int typeId, int pageNum) {
        Pageable pageable = new PageRequest(0, MythConfig.HOT_GOODS_SIZE * pageNum);
        return goodsDao.getTypeGoods(typeId, pageable);
    }

    /**
     * 根据编号获取商品.
     *
     * @param goodId 编号
     * @return
     */
    public Goods getGoodsById(long goodId) {
        return goodsDao.getOne(goodId);
    }

    /**
     * 查询商品.
     *
     * @param query   查询条件
     * @param pageNum 查询
     * @return
     */
    public Page<Goods> searchGoods(String query, int pageNum) {
        query = query.trim();
        query = query.replace(" ", "%");
        query = "%" + query + "%";
        Pageable pageable = new PageRequest(pageNum, MythConfig.HOT_GOODS_SIZE);
        return goodsDao.getSearchGoods(query, pageable);
    }

    public Page<Goods> historySearchGoods(String query, int pageNum) {
        query = query.trim();
        query = query.replace(" ", "%");
        query = "%" + query + "%";
        Pageable pageable = new PageRequest(0, MythConfig.HOT_GOODS_SIZE * pageNum);
        return goodsDao.getSearchGoods(query, pageable);
    }

    /**
     * 首页页面返回商品.
     *
     * @param pageNum
     * @return
     */
    public Page<Goods> historyGoods(int pageNum) {
        Pageable pageable = new PageRequest(0, MythConfig.HOT_GOODS_SIZE * pageNum);
        return goodsDao.getHotGoods(pageable);
    }

    /**
     * 热搜提示.
     * @return
     */
    public List<String> queryGoods() {
        List<String> result = new ArrayList<>();
        ZSetOperations<String, String> zset = stringRedisTemplate.opsForZSet();
        Set<String> set = zset.reverseRangeByScore("search_type", 0, Long.MAX_VALUE);
        int index = 0;
        for (String s : set) {
            if (index < 10) {
                result.add(s);
                index++;
            } else {
                return result;
            }
        }
        return result;
    }

    public int getTedayImportCount() {
        return goodsDao.getTedayImportCount();
    }

    public long getGoodsCount() {
        return goodsDao.getGoodsCount();
    }

}
